package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.ZiXunFeiLeiAdapter;
import com.tangkun.environment.entity.xinwen.TypeReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 咨询分类
 */

public class ZiXunFeiLeiActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;

    /**
     * 选中的ID
     */
    private String selectedIds = "";
    /**
     * 数据源
     */
    /**
     * 咨询分类
     */
    private List<TypeReturn.DataBean> dataList = new ArrayList<TypeReturn.DataBean>();
    /**
     * 适配器
     */
    private ZiXunFeiLeiAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        initView();
        queryType("question-type");
    }

    private void initView() {
        mContext = this;
        selectedIds = getIntent().getStringExtra("selectedIds");
        title.setText("咨询分类");

        adapter = new ZiXunFeiLeiAdapter(ZiXunFeiLeiActivity.this, dataList);
        customListView.setAdapter(adapter);
        customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //选中的ID和名字
                String ids = dataList.get(position - 1).ids;
                String name = dataList.get(position - 1).val;
                //修改传染病成功后调用下面代码
                Intent intent = new Intent();
                intent.putExtra("name", name);
                intent.putExtra("id", ids);
                setResult(RESULT_OK, intent);
                Utils.hideSoftKeyboard(ZiXunFeiLeiActivity.this, back);
                JumpHelper.finish(mContext);
            }
        });
    }


    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                JumpHelper.finish(mContext);
                break;
        }
    }

    /**
     * 获取新闻类别
     *
     * @param numbers 新闻类别：news-type；咨询分类：question-type
     */
    private void queryType(final String numbers) {
        fromNetwork.queryType(numbers).compose(new RxActivityHelper<TypeReturn>().ioMain(ZiXunFeiLeiActivity.this, false))
                .subscribe(new MySubscribe<TypeReturn>(ZiXunFeiLeiActivity.this, false) {
                    @Override
                    protected void success(TypeReturn resultBean) {
                        if (resultBean.data != null) {
                            dataList.clear();
                            dataList.addAll(resultBean.data);

                            //历史选中项
                            if (!TextUtils.isEmpty(selectedIds)) {
                                String[] id = selectedIds.split(",");
                                for (int i = 0; i < id.length; i++) {
                                    for (int j = 0; j < dataList.size(); j++) {
                                        if (!TextUtils.isEmpty(String.valueOf(dataList.get(j).ids))) {
                                            if (String.valueOf(dataList.get(j).ids).equals(id[i])) {
                                                dataList.get(j).isSelected = true;
                                            }
                                        }
                                    }
                                }
                            }

                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }
}
